<!-- src/views/login/index.vue -->
<template>
  <div class="login-container">
    <!-- 左侧表单区域 -->
    <div class="login-left">
      <div class="login-form-wrapper">
        <!-- Logo -->
        <div class="logo-section">
          <div class="logo-icon">
            <svg viewBox="0 0 24 24" fill="currentColor">
              <path d="M12 2L2 7v10c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V7l-10-5z"/>
            </svg>
          </div>
          <span class="logo-text">VCool Admin</span>
        </div>

        <!-- 标题 -->
        <h1 class="welcome-title">欢迎来到管理平台</h1>
        <p class="welcome-subtitle">输入您的账户信息以登录系统</p>
        
        <!-- 模拟账号提示 -->
        <div class="mock-accounts">
          <h3>测试账号（点击快速填入）</h3>
          <div class="account-list">
            <div class="account-item" @click="fillAccount('admin', '123456')">
              <div class="account-info">
                <span class="role">👨‍💼 管理员</span>
                <span class="desc">拥有所有菜单权限</span>
              </div>
              <span class="account">admin / 123456</span>
            </div>
            <div class="account-item" @click="fillAccount('user', '123456')">
              <div class="account-info">
                <span class="role">👤 普通用户</span>
                <span class="desc">部分菜单权限</span>
              </div>
              <span class="account">user / 123456</span>
            </div>
            <div class="account-item" @click="fillAccount('guest', '123456')">
              <div class="account-info">
                <span class="role">👥 访客</span>
                <span class="desc">最少菜单权限</span>
              </div>
              <span class="account">guest / 123456</span>
            </div>
          </div>
        </div>

        <!-- 登录表单 -->
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          @keyup.enter="handleLogin"
          class="login-form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="邮箱/手机号码"
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="输入您的密码"
              size="large"
              show-password
              class="custom-input"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <div class="form-options">
            <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
            <a href="#" class="forgot-link">忘记密码？</a>
          </div>

          <el-button
            type="primary"
            size="large"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            登 录
          </el-button>
        </el-form>

        <!-- 底部链接 -->
        <div class="form-footer">
          <span>还没账号？</span>
          <a href="#" class="register-link">注册</a>
        </div>

        <!-- 第三方登录 -->
        <div class="divider">
          <span>其它登录方式</span>
        </div>

        <div class="social-login">
          <el-button class="social-btn" circle>
            <el-icon><svg viewBox="0 0 24 24"><path fill="currentColor" d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/></svg></el-icon>
          </el-button>
          <el-button class="social-btn" circle>
            <el-icon><svg viewBox="0 0 24 24"><path fill="currentColor" d="M23.5 12.2c0-.8-.1-1.5-.2-2.2H12v4.2h6.5c-.3 1.5-1.2 2.8-2.5 3.6v3h4c2.3-2.1 3.5-5.3 3.5-8.6z"/><path fill="currentColor" d="M12 24c3.2 0 5.9-1.1 7.9-2.9l-4-3c-1.1.7-2.5 1.2-3.9 1.2-3 0-5.5-2-6.4-4.7H1.4v3.1C3.4 21.4 7.4 24 12 24z"/><path fill="currentColor" d="M5.6 14.6c-.2-.7-.4-1.4-.4-2.1s.1-1.4.4-2.1V7.3H1.4C.5 9.1 0 11 0 12s.5 2.9 1.4 4.6l4.2-3.1z"/><path fill="currentColor" d="M12 4.8c1.7 0 3.2.6 4.4 1.7l3.3-3.3C17.9 1.2 15.2 0 12 0 7.4 0 3.4 2.6 1.4 6.3l4.2 3.1c.9-2.7 3.4-4.7 6.4-4.7z"/></svg></el-icon>
          </el-button>
        </div>
      </div>
    </div>

    <!-- 右侧插画区域 -->
    <div class="login-right">
      <div class="right-content">
        <div class="brand-section">
          <h2 class="brand-title">VCool</h2>
          <p class="brand-slogan">让设计更简单</p>
          <p class="brand-description">Make the design easier</p>
        </div>

        <!-- 插画 -->
        <div class="illustration">
          <svg viewBox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
            <!-- 背景装饰圆 -->
            <circle cx="650" cy="100" r="80" fill="rgba(255,255,255,0.1)"/>
            <circle cx="120" cy="500" r="60" fill="rgba(255,255,255,0.08)"/>
            
            <!-- 人物和桌子 -->
            <g>
              <!-- 桌子 -->
              <rect x="250" y="380" width="300" height="15" rx="7" fill="#5B6FE8"/>
              <rect x="270" y="395" width="10" height="120" rx="5" fill="#4A5FCF"/>
              <rect x="520" y="395" width="10" height="120" rx="5" fill="#4A5FCF"/>
              
              <!-- 电脑 -->
              <rect x="320" y="280" width="180" height="120" rx="8" fill="#374785"/>
              <rect x="330" y="290" width="160" height="90" rx="4" fill="#5B6FE8" opacity="0.3"/>
              
              <!-- 盆栽 -->
              <ellipse cx="590" cy="370" rx="25" ry="15" fill="#5B6FE8"/>
              <path d="M570 340 Q580 310 590 340 Q600 310 610 340 Q600 360 590 370 Q580 360 570 340" fill="#4CAF50"/>
              
              <!-- 人物身体 -->
              <ellipse cx="400" cy="310" rx="45" ry="55" fill="#E91E63"/>
              
              <!-- 人物头部 -->
              <circle cx="400" cy="240" r="35" fill="#FDB5B5"/>
              
              <!-- 头发 -->
              <path d="M365 235 Q370 200 400 195 Q430 200 435 235 Z" fill="#2C3E50"/>
              <circle cx="405" cy="210" r="18" fill="#2C3E50"/>
              
              <!-- 手臂 -->
              <ellipse cx="360" cy="320" rx="15" ry="45" fill="#FDB5B5" transform="rotate(-30 360 320)"/>
              <ellipse cx="440" cy="320" rx="15" ry="45" fill="#FDB5B5" transform="rotate(30 440 320)"/>
              
              <!-- 消息图标 -->
              <circle cx="500" cy="220" r="28" fill="#5B6FE8"/>
              <path d="M490 220 L495 225 L510 210" stroke="white" stroke-width="3" fill="none" stroke-linecap="round"/>
            </g>
          </svg>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { useUserStore } from "@/store/user";
import { User, Lock, HomeFilled } from '@element-plus/icons-vue';

const router = useRouter();
const userStore = useUserStore();

const loginFormRef = ref();
const loading = ref(false);

const loginForm = reactive({
  username: "",
  password: "",
  remember: false
});

const loginRules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

const handleLogin = async () => {
  if (!loginFormRef.value) return;

  const valid = await loginFormRef.value.validate();
  if (!valid) return;

  loading.value = true;

  try {
    await userStore.login(loginForm);
    ElMessage.success("登录成功");
    
    // 强制刷新页面以确保路由和菜单正确加载
    window.location.href = "/";
  } catch (error) {
    ElMessage.error(error.message || "登录失败");
  } finally {
    loading.value = false;
  }
};

// 快速填入账号
const fillAccount = (username, password) => {
  loginForm.username = username;
  loginForm.password = password;
};
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* 左侧表单区域 */
.login-left {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  background: #ffffff;
  position: relative;
}

.login-form-wrapper {
  width: 100%;
  max-width: 420px;
}

/* Logo */
.logo-section {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 48px;
}

.logo-icon {
  width: 42px;
  height: 42px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.logo-icon svg {
  width: 24px;
  height: 24px;
}

.logo-text {
  font-size: 24px;
  font-weight: 700;
  color: #202124;
  letter-spacing: -0.5px;
}

/* 标题 */
.welcome-title {
  font-size: 32px;
  font-weight: 700;
  color: #202124;
  margin: 0 0 12px 0;
  letter-spacing: -0.5px;
}

.welcome-subtitle {
  font-size: 15px;
  color: #5f6368;
  margin: 0 0 40px 0;
  line-height: 1.5;
}

/* 模拟账号提示 */
.mock-accounts {
  background: #f8f9fa;
  border: 1px solid #e8eaed;
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 24px;
}

.mock-accounts h3 {
  font-size: 14px;
  color: #5f6368;
  margin: 0 0 12px 0;
  font-weight: 600;
}

.account-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.account-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  background: white;
  border: 1px solid #e8eaed;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.account-item:hover {
  border-color: #1a73e8;
  background: #f8f9ff;
  transform: translateX(2px);
}

.account-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.account-item .role {
  font-size: 13px;
  color: #1a73e8;
  font-weight: 600;
}

.account-item .desc {
  font-size: 11px;
  color: #80868b;
}

.account-item .account {
  font-size: 12px;
  color: #5f6368;
  font-family: monospace;
  white-space: nowrap;
}

/* 表单 */
.login-form {
  margin-bottom: 24px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.custom-input .el-input__wrapper) {
  padding: 12px 16px;
  border-radius: 10px;
  border: 1.5px solid #e8eaed;
  box-shadow: none;
  transition: all 0.3s ease;
}

:deep(.custom-input .el-input__wrapper:hover) {
  border-color: #dadce0;
}

:deep(.custom-input.is-focus .el-input__wrapper) {
  border-color: #1a73e8;
  box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.1);
}

:deep(.custom-input .el-input__prefix) {
  color: #5f6368;
  font-size: 18px;
}

:deep(.custom-input.is-focus .el-input__prefix) {
  color: #1a73e8;
}

/* 表单选项 */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

:deep(.el-checkbox__label) {
  font-size: 14px;
  color: #5f6368;
}

.forgot-link {
  font-size: 14px;
  color: #1a73e8;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s;
}

.forgot-link:hover {
  color: #174ea6;
}

/* 登录按钮 */
.login-button {
  width: 100%;
  height: 48px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.5px;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(102, 126, 234, 0.5);
}

.login-button:active {
  transform: translateY(0);
}

/* 底部链接 */
.form-footer {
  text-align: center;
  margin-bottom: 32px;
  font-size: 14px;
  color: #5f6368;
}

.register-link {
  color: #1a73e8;
  text-decoration: none;
  font-weight: 600;
  margin-left: 6px;
  transition: color 0.2s;
}

.register-link:hover {
  color: #174ea6;
}

/* 分割线 */
.divider {
  position: relative;
  text-align: center;
  margin: 32px 0 24px;
}

.divider::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  width: 100%;
  height: 1px;
  background: #e8eaed;
}

.divider span {
  position: relative;
  background: white;
  padding: 0 16px;
  font-size: 13px;
  color: #80868b;
}

/* 第三方登录 */
.social-login {
  display: flex;
  gap: 16px;
  justify-content: center;
}

.social-btn {
  width: 48px;
  height: 48px;
  background: #f8f9fa;
  border: 1.5px solid #e8eaed;
  transition: all 0.3s ease;
}

.social-btn:hover {
  background: white;
  border-color: #1a73e8;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.social-btn .el-icon {
  font-size: 20px;
}

/* 右侧插画区域 */
.login-right {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  position: relative;
  overflow: hidden;
}

.right-content {
  width: 100%;
  max-width: 600px;
  position: relative;
  z-index: 1;
}

/* 品牌区域 */
.brand-section {
  text-align: center;
  margin-bottom: 60px;
}

.brand-title {
  font-size: 56px;
  font-weight: 800;
  color: white;
  margin: 0 0 16px 0;
  letter-spacing: -1px;
  text-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.brand-slogan {
  font-size: 28px;
  color: white;
  margin: 0 0 8px 0;
  font-weight: 600;
}

.brand-description {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.9);
  margin: 0;
  font-weight: 400;
  letter-spacing: 1px;
}

/* 插画 */
.illustration {
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
}

.illustration svg {
  width: 100%;
  height: auto;
  filter: drop-shadow(0 20px 40px rgba(0, 0, 0, 0.2));
}

/* 返回首页按钮 */
.home-button {
  position: absolute;
  top: 30px;
  right: 30px;
  color: white;
  font-size: 15px;
  font-weight: 500;
  padding: 10px 20px;
  transition: all 0.3s ease;
}

.home-button:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translateY(-2px);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login-right {
    display: none;
  }
  
  .login-left {
    flex: 1;
  }
}

@media (max-width: 640px) {
  .login-left {
    padding: 24px;
  }
  
  .welcome-title {
    font-size: 28px;
  }
  
  .logo-text {
    font-size: 20px;
  }
}
</style>
